<?php

namespace App\Jobs;

use App\Http\Models\Order;
use App\Http\Models\ZtkTask;
use App\Http\Models\ZtkTaskBlackUser;
use App\Http\Models\ZtkUser;
use App\Http\Models\ZtkUserAutoOrder;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class AutoOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    private $task_id;


    public function __construct($task_id)
    {
        $this->task_id = $task_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        DB::beginTransaction();
        try {
            $task = ZtkTask::find($this->task_id);

            //指派单 加急单 不接
            if(!empty($task) && $task->type != 2 && $task->send_date != 4)
            {
                $userList = ZtkUserAutoOrder::valid()->orderBy('sorting','asc')->limit($task->total_num)->get();
                foreach($userList as $key=>$value)
                {
                    $check = $this->checkAutoOrder($task,$value->user_id);
                    if($check == false) continue;
                    Order::create([
                        'user_id' => $value->user_id,
                        'publish_user_id' => $task->user_id,
                        'task_id' => $task->id,
                        'price'   => $task->price,
                        'type'    => $task->type,
                        'remark'  => '自动接单',
                        'yes_date'=> Carbon::now(),
                    ]);
                    $task->receive_num = $task->receive_num + 1;
                    $task->stock_num = $task['stock_num'] - 1;
                    if($task->stock_num < 1){
                        $task->status = 3;//已满额
                    }
                    $task->save();
                }
            }
            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
        }
    }

    public function checkAutoOrder($task,$userId)
    {
        if($task->status != 1 ){ //任务不是进行中，无法接单
            return false;
        }

        if($task->stock_num < 1 ){ //任务已满额
            return false;
        }

        if($task->user_id === $userId){ //自己发布的任务不能接单
            return false;
        }

        $order = Order::where(['user_id' => $userId, 'task_id' => $task->id])->first(); //已经接过该任务了
        if($order){
            return false;
        }

        $user = ZtkUser::find($userId); //未认证的联盟不能接单
        if($user->is_union_auth == 0){
            return false;
        }

        $blackuser = ZtkTaskBlackUser::where(['black_user_id' => $userId, 'task_id' => $task->id])->exists(); //在黑名单中
        if($blackuser){
            return false;
        }

        return true;
    }

}
